﻿<html>
<head>
    <title>组织结构管理</title>
    <meta charset="UTF-8" />

    <link rel="stylesheet" type="text/css" href="themes/default/easyui.css" />
    <link href="../../DataUser/Style/admin/skins/context.standalone.css" rel="stylesheet"
          type="text/css" />
    <script src="../WF/Admin/CCBPMDesigner/js/context.js" type="text/javascript"></script>
    <script type="text/javascript" src="../WF/Scripts/jquery-1.11.0.min.js"></script>
    <script type="text/javascript" src="../WF/Scripts/jquery/jquery.easyui.min.js"></script>
    <script src="../WF/Scripts/jquery/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../WF/Scripts/QueryString.js" type="text/javascript"></script>
    <link href="../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <!-- 引入新版本的类库. -->
    <script src="../WF/Scripts/config.js" type="text/javascript"></script>
    <script src="../WF/Comm/Gener.js" type="text/javascript"></script>

    <script src="../WF/Scripts/bootstrap/bootstrap-treeview/src/js/bootstrap-treeview.js" type="text/javascript"></script>
    <link href="../WF/Scripts/bootstrap/bootstrap-treeview/src/css/bootstrap-treeview.css" rel="stylesheet" type="text/css" />
    <script src="../WF/Scripts/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <link href="../WF/Scripts/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="../WF/Scripts/bootstrap/css/font-awesome.css" rel="stylesheet" type="text/css" />

    <script src="../WF/Scripts/bootstrap/bootstrap-table/src/bootstrap-table.js" type="text/javascript"></script>
    <link href="../WF/Scripts/bootstrap/bootstrap-table/src/bootstrap-table.css" rel="stylesheet" type="text/css" />
    <script src="../WF/Scripts/bootstrap/bootstrap-table/src/locale/bootstrap-table-zh-CN.js" type="text/javascript"></script>
    <script src="../WF/Scripts/bootstrap/BootstrapUIDialog.js" type="text/javaScript"></script>
    <!--引入zTree树. -->
    <link href="../WF/Scripts/zTree_v3/css/metroStyle/metroStyle.css" rel="stylesheet"
          type="text/css" />
    <script src="../WF/Scripts/zTree_v3/js/jquery.ztree.all.min.js" type="text/javascript"></script>
    <script src="../WF/Scripts/zTree_v3/js/jquery.ztree.core.js" type="text/javascript"></script>
    <script src="../WF/Scripts/zTree_v3/js/jquery.ztree.excheck.js" type="text/javascript"></script>
    <script src="../WF/Scripts/zTree_v3/js/jquery.ztree.exedit.js" type="text/javascript"></script>
    <style type="text/css">

        .small-menu {
            position: absolute;
            width: 120px;
            z-index: 99999;
            border: solid 1px #CCC;
            background: #EEE;
            padding: 0px;
            margin: 0px;
            display: none;
        }

            .small-menu li {
                list-style: none;
                padding: 0px;
                margin: 0px;
            }

                .small-menu li A {
                    color: #333;
                    text-decoration: none;
                    display: block;
                    line-height: 20px;
                    height: 20px;
                    background-position: 6px center;
                    background-repeat: no-repeat;
                    outline: none;
                    padding: 1px 5px;
                    padding-left: 28px;
                }

                    .small-menu li a:hover {
                        color: #FFF;
                        background-color: #3399FF;
                    }

        .small-menu-separator {
            padding-bottom: 0;
            border-bottom: 1px solid #DDD;
        }

        .small-menu LI.edit A {
            background-image: url(images/page_white_edit.png);
        }

        .small-menu LI.cut A {
            background-image: url(images/cut.png);
        }

        .small-menu LI.copy A {
            background-image: url(images/page_white_copy.png);
        }

        .small-menu LI.paste A {
            background-image: url(images/page_white_paste.png);
        }

        .small-menu LI.delete A {
            background-image: url(images/page_white_delete.png);
        }

        .small-menu LI.quit A {
            background-image: url(images/door.png);
        }
    </style>


    <script type="text/javascript">
        var deptNo = "";
        var webUser = new WebUser();

        var setting = {
            view: {
                dblClickExpand: false, //双击节点时，是否自动展开父节点的标识
                showLine: true, //是否显示节点之间的连线
                fontCss: {}, //字体样式函数
                selectedMulti: false //设置是否允许同时选中多个节点
            },
            check: {
                chkboxType: { "Y": "ps", "N": "ps" },
                chkboxType: { "Y": "", "N": "" },
                chkStyle: "checkbox", //复选框类型
                enable: false //每个节点上是否显示 CheckBox
            },
            edit: {
                enable: true,
                editNameSelectAll: true,
                showRemoveBtn: false,
                showRenameBtn: false,
                removeTitle: "remove",
                renameTitle: "rename"
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pid",
                    rootPId: ""
                }
            },
            callback: {

                onClick: function (event, treeId, treeNode) {
                    deptNo = treeNode.id;
                    BindEmps();
                },
                onRightClick: OnRightClick
            } //这里是节点点击事件
        };

        //右键菜单功能实现
        (function ($, undefined) {
            $.fn.popupSmallMenu = function (options) {
                var $currMenu = $(this),
                    defaultOptions = {
                        event: null,
                        onClickItem: null
                    },
                    options = $.extend(defaultOptions, options);

                var _smallMenu = {
                    popupSmallMenu: function () {
                        this._bindItemClick();
                        this._bindMenuEvent();
                        this._showMenu();
                        return $currMenu;
                    },
                    _bindMenuEvent: function () {
                        var thiz = this;
                        $currMenu.hover(function () {
                        }, function () {
                            thiz._unBindItemClick();
                            $currMenu.hide();
                        });

                        $currMenu.click(function () {
                            thiz._unBindItemClick();
                            $currMenu.hide();
                        });
                    },
                    _showMenu: function () {
                        if (!options.event) {
                            alert('请传入鼠标事件');
                        }
                        $currMenu.css({
                            top: options.event.clientY + "px",
                            left: options.event.clientX + "px",
                            display: "block"
                        });
                    },
                    _bindItemClick: function () {
                        $currMenu.find('li').each(function (index, obj) {
                            var $li = $(obj);
                            var itemIden = $li.attr('class');
                            $li.bind('click', function (event) {
                                event.stopPropagation();
                                if (options.onClickItem
                                    && typeof options.onClickItem === 'function') {
                                    options.onClickItem(itemIden);
                                }
                            });
                        });
                    }
                    ,
                    _unBindItemClick: function () {
                        $currMenu.find('li').each(function (index, obj) {
                            $(obj).unbind();
                        });
                    }
                };

                return _smallMenu.popupSmallMenu();
            }
        })(jQuery);
        //右键点击
        function OnRightClick(event, treeId, treeNode) {
            zTreeObj.selectNode(treeNode);
            if (treeNode) {
                //弹出菜单
                $("#menu").popupSmallMenu({
                    event: event,
                    onClickItem: function (item) {
                        if (item == "moveUpDept") {
                            moveUpDept(treeNode, item);
                        } else if (item == "moveDownDept") {
                            moveDownDept(treeNode, item);
                        }

                    }
                });

            }
        }

        //上移部门
        function moveUpDept(treeNode, item) {
            var dept = new Entity("BP.GPM.Dept", treeNode.id);
            var data = dept.DoMethodReturnString("DoUp");
            window.location.reload();
        }

        //下移部门
        function moveDownDept(treeNode, item) {
            var dept = new Entity("BP.GPM.Dept", treeNode.id);
            var data = dept.DoMethodReturnString("DoDown");
            window.location.reload();
        }


        //初始化
        $(function () {

            $("#pageloading").show();

            var handler = new HttpHandler("BP.WF.HttpHandler.GPMPage");
            var ens = handler.DoMethodReturnJSON("Organization_Init");

            var jsonTree = createJsonToTree(ens);
            zTreeObj = $.fn.zTree.init($("#regionZTree"), setting, jsonTree);
            fillter(zTreeObj); //设置展开前三级
            //绑定帮助信息.
            var html = " <h1 style='padding-left:5px'>请点击左侧节点显示数据。</h1>";
            $("#Emps").html(html);
            $("#pageloading").hide();
            //鼠标移开时右键菜单消失
            $(document).on('mouseleave ', '.dropdown-menu', function () {
                $(".dropdown-menu").hide();
                $.learunindex.destroy
            });
        });

        //设置展开前三级
        function fillter(zTreeObj) {
            //获得树形图对象
            var nodeList = zTreeObj.getNodes();　　　　　　 //展开第一个根节点
            for (var i = 0; i < nodeList.length; i++) { //设置节点展开第二级节点
                zTreeObj.expandNode(nodeList[i], true, false, true);
                //					var nodespan = nodeList[i].children;
                //					for(var j = 0; j < nodespan.length; j++) { //设置节点展开第三级节点
                //						zTreeObj.expandNode(nodespan[j], true, false, true);
                //					}
            }
        }
        //转换ztree需要的数据格式
        var treeJson = [];
        function createJsonToTree(jsonArray) {

            for (var i = 0; i < jsonArray.length; i++) {
                var data = jsonArray[i];
                if (data.No == "" || data.No == undefined)
                    continue;
                treeJson.push({
                    "id": data.No,
                    "pid": data.ParentNo,
                    "name": data.Name,
                    "idx": data.Idx

                });
            }
            return treeJson;
        }


        //绑定人员.
        function BindEmps() {

            var dept = new Entity("BP.GPM.Dept", deptNo);

            var html = "";
            html += "<div class='container'>";
            html += "<div class='fixed-table-header'>";
            html += "<table class='table table-hover table-striped' style='width:90%;' >";
            html += "<tr><h5><a href=\"javascript:EditDept('" + dept.No + "')\" >" + dept.Name + " </a> - <a href='javascript:EditEmp()'>新建人员</a> -  <a href='javascript:RefEmps()'>关联兼职</a>  </h5></tr>";
            html += "<thead ><tr> ";
            html += "<th style='background-color:#F2F2F2;'>序</th>";
            html += "<th style='background-color:#F2F2F2;'>编号</th>";
            html += "<th style='background-color:#F2F2F2;'>名称</th>";
            html += "<th style='background-color:#F2F2F2;'>主部门</th>";
            html += "<th style='background-color:#F2F2F2;'>邮件</th>";
            html += "<th style='background-color:#F2F2F2;'>电话</th>";
            html += "<th style='background-color:#F2F2F2;'>操作</th>";
            html += "<th style='background-color:#F2F2F2;'>位置</th>";
            html += "</tr></thead>";

            var emps = new Entities("BP.GPM.Emps");
            emps.Retrieve("FK_Dept", dept.No, "Idx");


            var empNos = ",";
            html += "<tbody>";
            for (var i = 0; i < emps.length; i++) {
                var emp = emps[i];
                html += "<tr ondblclick=\"EditEmp('" + emp.No + "')\" >";
                html += "<td class='Idx' >" + (i + 1) + "</td>";
                html += "<td>" + emp.No + "</td>";
                html += "<td><a href=\"javascript:EditEmp('" + emp.No + "')\">" + emp.Name + "</a></td>";
                html += "<td>" + dept.Name + "</td>";
                html += "<td>" + emp.Email + "</td>";
                html += "<td>" + emp.Tel + "</td>";
                html += "<td><a href=\"javascript:Del('" + emp.No + "')\" ><img src='../WF/Img/Btn/Delete.gif'>删除</a></td>";
                html += "<td><img src='../WF/Img/Btn/Up.GIF' onclick=\"Up('" + emp.No + "')\" alt='上移' />-<img src='../WF/Img/Btn/Down.gif' onclick=\"Down('" + emp.No + "')\" alt='下移' /> </td>";
                html += "</tr>";
                empNos += emp.No + ',';
            }

            var deptEmps = new Entities("BP.GPM.DeptEmps");
            deptEmps.Retrieve("FK_Dept", deptNo);

            var num = emps.length;

            for (var i = 0; i < deptEmps.length; i++) {

                var deptEmp = deptEmps[i];
                if (empNos.indexOf(',' + deptEmp.FK_Emp + ',') != -1)
                    continue;

                var emp = new Entity("BP.GPM.Emp", deptEmp.FK_Emp);

                html += "<tr ondblclick=\"EditEmp('" + emp.No + "')\" >";
                html += "<td class='Idx' >" + num + "</td>";
                html += "<td>" + emp.No + "</td>";
                html += "<td><a href=\"javascript:EditEmp('" + emp.No + "')\">" + emp.Name + "</a></td>";
                html += "<td>兼职</td>";
                html += "<td>" + emp.Email + "</td>";
                html += "<td>" + emp.Tel + "</td>";
                html += "<td><a href=\"javascript:Remove('" + emp.No + "')\" >移除<img src='../WF/Img/Btn/Delete.gif'></a></td>";
                html += "<td><img src='../WF/Img/Btn/Up.GIF' onclick=\"Up('" + emp.No + "')\" alt='上移' />-<img src='../WF/Img/Btn/Down.gif' onclick=\"Down('" + emp.No + "')\" alt='下移' /> </td>";
                html += "</tr>";
                num++;

                empNos += emp.No + ',';
            }



            //找一下 Port_DeptEmpStation 里面有的数据，但是在 Port_DeptEmp没有的就，插入进去.

            var deptEmpStations = new Entities("BP.GPM.DeptEmpStations");
            deptEmpStations.Retrieve("FK_Dept", deptNo);
            var isHave = false;
            for (var i = 0; i < deptEmpStations.length; i++) {

                var en = deptEmpStations[i];

                if (empNos.indexOf(',' + en.FK_Emp + ',') == -1) {
                    /*插入里面去.*/

                    var deptEmp = new Entity("BP.GPM.DeptEmp");
                    deptEmp.SetPKVal(en.FK_Dept + "_" + en.FK_Emp);
                    deptEmp.FK_Emp = en.FK_Emp;
                    deptEmp.FK_Dept = en.FK_Dept;
                    deptEmp.Insert();
                    isHave = true;
                }
            }

            if (isHave)
                BindEmps();

            html += "</tbody>";
            html += "</table>";
            html += "</div>";
            html += "</div>";
            $("#Emps").html(html);
        }

        function Up(no) {
            var emp = new Entity("BP.GPM.Emp", no);
            var data = emp.DoMethodReturnString("DoUp");
            BindEmps();
        }
        function Down(no) {

            var emp = new Entity("BP.GPM.Emp", no);
            var data = emp.DoMethodReturnString("DoDown");
            BindEmps();
        }

        function EditDept(no) {
            var url = '../WF/Comm/En.htm?EnName=BP.GPM.Dept&No=' + no;
            OpenBootStrapModal(url, "eudlgframe", "编辑", '800', '500', "icon-edit", true, null, null);
        }

        function EditEmp(no) {

            if (no == undefined) {
                var url = '../WF/Comm/En.htm?EnName=BP.GPM.Emp&FK_Dept=' + deptNo;
                OpenBootStrapModal(url, "eudlgframe", "新增", '800', '500', "icon-edit", true, null, null);
                return;
            }

            var url = '../WF/Comm/En.htm?EnName=BP.GPM.Emp&No=' + no;
            OpenBootStrapModal(url, "eudlgframe", "编辑", '800', '500', "icon-edit", true, null, null);
        }

        function RefEmps() {

            var url = "../WF/Comm/RefFunc/BranchesAndLeaf.htm?EnName=BP.GPM.Dept&Dot2DotEnsName=BP.GPM.DeptEmps&Dot2DotEnName=BP.GPM.DeptEmp&AttrOfOneInMM=FK_Dept&AttrOfMInMM=FK_Emp&EnsOfM=BP.Port.Emps&DefaultGroupAttrKey=FK_Dept&No=" + deptNo + "&PKVal=" + deptNo + "&s=0.13113811473063341";
            OpenBootStrapModal(url, "eudlgframe", "关联人员", '1000', '500', "icon-edit", true, null, null);
        }

        function Del(no) {

            if (confirm('您确定要删除该人员吗？') == false)
                return;

            //取消人员部门管理关系
            var en = new Entity("BP.GPM.DeptEmp");
            en.SetPKVal(deptNo + "_" + no);
            en.Delete();

            var en = new Entity("BP.GPM.Emp", no);
            en.Delete();

            //   var en = new Entity("BP.GPM.DeptEmpStation");
            //  en.Delete("FK_Dept",);
            // SELECT * FROM Port_DeptEmpStation WHERE FK_Dept+'_'+FK_Emp not in (select MyPK FROM PORT_DEPTEMP)

            BindEmps();
        }

        function Remove(no) {

            if (confirm('您确定要从该部门中移除吗？') == false)
                return;

            var en = new Entity("BP.GPM.DeptEmp");
            en.SetPKVal(deptNo + "_" + no);
            en.Delete();

            BindEmps();
        }

    </script>
    <style type="text/css">
    </style>
</head>
<body class="easyui-layout">
    <div id="pageloading">
    </div>
    <div data-options="region:'west',split:true" style="width: 200px; padding: 1px; overflow: hidden;">
        <div style="width: 100%; height: 95%; overflow: auto;">
            <ul id="regionZTree" class="ztree" data-options="animate:false,dnd:false"></ul>
        </div>
        <ul id="menu" class="small-menu">
            <li class="moveUpDept"><a href="#">上移</a></li>
            <li class="moveDownDept"><a href="#">下移</a></li>
            <!-- <li class="copy"><a href="#">删除</a></li>
             <li class="small-menu-separator"></li>
             <li class="delete"><a href="#"><span class="icon icon-edit"></span>Zoom Out</a></li>-->
        </ul>
    </div>



    <div data-options="region:'center',split:true" style="width:90%;">
        <div id="Emps"></div>
    </div>

</body>
</html>
